<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

class ConnectionRouterImpl extends AbstractApplicationComponent
{

  protected $connects = array();

  /**
   * 
   * @var array
   */
  protected $parsed = array();

  /**
   * 
   * @var ICache
   */
  protected $cache;

  /**
   * 
   * @var string
   */
  protected $cacheid = 'Simplify/Router/Routes/Connections';

  public function build($route, $params)
  {
  }

  public function connect($route, $defaults = array(), $match = array())
  {
    $words = preg_split('#/#', $route, - 1, PREG_SPLIT_NO_EMPTY);
    
    $regex = array();
    $names = array();
    $index = 0;
    
    foreach ($words as $word) {
      if (preg_match('/^\*$/', $word)) {
        $regex[] = '(?:/(.*))?';
        
        $names[$index] = 'params';
        
        $index ++;
      }
      
      else if (preg_match('/^:(.+)$/', $word, $wildcard)) {
        $wildcard = $wildcard[1];
        $names[$index] = $wildcard;
        
        if (isset($match[$wildcard])) {
          $regex[] = '(?:/(' . $match[$wildcard] . '))?';
        }
        else {
          $regex[] = '(?:/([^/]+))?';
        }
        
        $index ++;
      }
      
      else {
        $regex[] = '/' . $word;
      }
    }
    
    $regex = '#^' . implode('', $regex) . '/*$#';
    
    $connect = array(
      'route' => $route, 'regex' => $regex, 'names' => $names, 'defaults' => $defaults, 'match' => $match 
    );
    
    $this->connects[] = $connect;
  }

  public function parse($route)
  {
    $i = 0;
    $c = count($this->connects);
    
    while ($i < $c && ! preg_match($this->connects[$i]['regex'], $route, $r)) {
      $i ++;
    }
    
    if ($i >= $c) {
      throw new RouterException('No matching route found');
    }
    
    array_shift($r);
    
    $connect = $this->connects[$i];
    
    $data = $connect['defaults'];
    foreach ($connect['names'] as $key => $name) {
      $value = getParam($r, $key, getParam($connect['defaults'], $name));
      
      if ($name == 'params') {
        $value = ( array ) explode('/', $value);
      }
      
      $data[$name] = $value;
    }
    
    foreach ($connect['defaults'] as $name => $value) {
      if (preg_match_all('/\:([^\/]+)/', $value, $r)) {
        foreach ($r[1] as $wildcard) {
          $data[$name] = str_replace(':' . $wildcard, $data[$wildcard], $data[$name]);
        }
      }
    }
    
    $base = $this->config->get('routing:home:dir');
    $data['path'] = $base . (isset($data['path']) ? $data['path'] : '/');
    $file = $data['path'] . '/' . $data['controller'] . '_controller.php';
    
    if (! file_exists($file)) {
      throw new RouterException('File not found: ' . $file);
    }
    
    require_once ($file);
    
    $class = Inflector::classify($data['controller'] . '_controller');
    
    if (! class_exists($class)) {
      throw new RouterException('Class not found: ' . $class);
    }
    
    return new ParameterHolder($data);
  }

}

?>